<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of thanhvien_model
 *
 * @author Bui
 */
class Thanhvien_model extends CI_Model {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    function login($username, $password) {
        $this->db->select('id', 'username', 'password');
        $this->db->from('gs7s_thanhvien');
        $this->db->where('username', $username);
        $this->db->where('password', md5($password));
        $this->db->limit(1);

        $query = $this->db->get();
        if ($query->num_rows == 1) {
            return $query->result();
        } else {
            return false;
        }
    }

    function active($username) {
        $data = array(
            'trangthai_id' => '2',
        );
        $this->db->where('username', $username);
        $this->db->update('gs7s_thanhvien', $data);
    }

    function self_load_id($email_address) {

        $this->db->where('email', $email_address);
        $this->db->from('gs7s_thanhvien');
        $this->db->select('id');
        $query = $this->db->get();
        return $query->row()->id;
        /*

          $this->db->select('id');
          $this->db->from('gs7s_thanhvien');
          $this->db->where('email',$email_address);
          $query = $this->db->get();
          return $query->row()->id;

         */
    }

    function deactive($username) {
        $data = array(
            'trangthai_id' => '1',
        );
        $this->db->where('username', $username);
        $this->db->update('gs7s_thanhvien', $data);
    }

    public function add_user() {
        $data = array(
            'tenhienthi' => $this->input->post('display_name'),
            'username' => $this->input->post('username'),
            'password' => md5($this->input->post('password')),
            'email' => $this->input->post('email_address'),
            'sodienthoai' => $this->input->post('phone_number'),
            'gioitinh' => $this->input->post('gioitinh'),
            'diachi' => $this->input->post('address'),
            'trangthai_id' => '1',
            'is7s' => 'NO',
        );

        //print_r($data);

        $this->db->set('ngaydangky', 'NOW()', FALSE);
        $this->db->insert('gs7s_thanhvien', $data);
    }

    public function self_add_user($data) {
        $thongtin = array(
            'username' => $data['username'],
            'password' => $data['password'],
            'gioitinh' => '2',
            'email' => $this->input->post('email_address'),
            'trangthai_id' => '1',
            'is7s' => 'NO',
        );

        $this->db->set('ngaydangky', 'NOW()', FALSE);
        $this->db->insert('gs7s_thanhvien', $thongtin);
    }

    function get_user($id) {
        $this->load->database();
        $query = $this->db->get_where('gs7s_thanhvien', array('id' => $id));
        return $query->row_array();
    }

    function get_user_by_username($username) {
        $this->load->database();
        $query = $this->db->get_where('gs7s_thanhvien', array('username' => $username));
        return $query->row_array();
    }

    function update_user($id, $user) {
        $this->load->database();
        $this->db->where('id', $id);
        $this->db->update('gs7s_thanhvien', $user);
    }

    function load_thanhvien($select, $where) {

        $this->db->select($select);
        $this->db->from('gs7s_thanhvien');
        $this->db->where($where['key'], $where['value']);
        $this->db->limit(1);
        $query = $this->db->get();
        if ($query->num_rows() == 0)
            return FALSE;
        return $query->result();
    }

    /*
     * Admin
     */

    function getAll() {
        $this->db->select('*');
        $this->db->from('gs7s_thanhvien');
//        $this->db->join('gs7s_thanhvien_diachi', 'gs7s_thanhvien.id = gs7s_thanhvien_diachi.thanhvien_id');
//        $this->db->join('gs7s_thanhvien_phanloai', 'gs7s_thanhvien.phanloai_id = gs7s_thanhvien_phanloai.id');
//        $this->db->join('gs7s_thanhvien_trangthai', 'gs7s_thanhvien.trangthai_id = gs7s_thanhvien_trangthai.id');
        $query = $this->db->get();
        return $query->result_array();
    }

    function load_signal_session() {
        $session_data = $this->session->userdata('logged_in');
        $id = $session_data['id'];
        $session = $this->db->get_where('gs7s_thanhvien', array('id' => $id), 1)->row()->session;
        $next_session = ($session + 1 > 1000) ? 1 : $session + 1;
        $this->db->update('gs7s_thanhvien', array('session' => $next_session), array('id' => $id), 1);
        return $session;
    }

}

?>
